cmake_minimum_required(VERSION 3.15)
project(CTPAPI)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置GBK编码，防止Visual Studio使用错误的编码
if(MSVC)
    # 检查编译器版本，VS2019及以上才支持source-charset和execution-charset
    if(MSVC_VERSION GREATER_EQUAL 1920)
        # 设置源文件字符集为GBK，执行字符集为GBK
        add_compile_options(/source-charset:gbk)
        add_compile_options(/execution-charset:gbk)
    endif()
    # 设置编译器警告等级
    add_compile_options(/W4)
    # 添加Windows相关定义
    add_compile_definitions(WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS)
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 包含当前目录的头文件
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# 查找Python - 兼容旧版本CMake
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.18")
    find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
    set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
    set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
else()
    # 对于较旧的CMake版本，使用传统方法查找Python
    find_package(PythonInterp 3 REQUIRED)
    find_package(PythonLibs 3 REQUIRED)
    set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
    set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES})
endif()

# 交易API库
add_library(thosttraderapi_se SHARED IMPORTED)
set_target_properties(thosttraderapi_se PROPERTIES
    IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/thosttraderapi_se.dll"
    IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/thosttraderapi_se.lib"
)

# 行情API库
add_library(thostmduserapi_se SHARED IMPORTED)
set_target_properties(thostmduserapi_se PROPERTIES
    IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/thostmduserapi_se.dll"
    IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/thostmduserapi_se.lib"
)

# 交易API Python包装器
add_library(_thosttraderapi MODULE
    thosttraderapi_wrap.cxx
    ${CMAKE_CURRENT_SOURCE_DIR}/tools/iconv_wrapper.cpp
)

# swig 生成的文件太大
target_compile_options(_thosttraderapi PRIVATE /bigobj)

target_include_directories(_thosttraderapi PRIVATE
    ${PYTHON_INCLUDE_DIRS}
)

target_link_libraries(_thosttraderapi PRIVATE
    thosttraderapi_se
    ${PYTHON_LIBRARIES}
)

set_target_properties(_thosttraderapi PROPERTIES
    PREFIX ""
    OUTPUT_NAME "_thosttraderapi"
    SUFFIX ".pyd"
)

# 行情API Python包装器
add_library(_thostmduserapi MODULE
    thostmduserapi_wrap.cxx
    ${CMAKE_CURRENT_SOURCE_DIR}/tools/iconv_wrapper.cpp
)
# swig 生成的文件太大
target_compile_options(_thostmduserapi PRIVATE /bigobj)

target_include_directories(_thostmduserapi PRIVATE
    ${PYTHON_INCLUDE_DIRS}
)

target_link_libraries(_thostmduserapi PRIVATE
    thostmduserapi_se
    ${PYTHON_LIBRARIES}
)

set_target_properties(_thostmduserapi PROPERTIES
    PREFIX ""
    OUTPUT_NAME "_thostmduserapi"
    SUFFIX ".pyd"
)

# 安装目标
install(TARGETS _thosttraderapi _thostmduserapi
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
)

install(FILES
    thosttraderapi.py
    thostmduserapi.py
    DESTINATION python
)

# 安装头文件
install(FILES
    ThostFtdcTraderApi.h
    ThostFtdcMdApi.h
    ThostFtdcUserApiDataType.h
    ThostFtdcUserApiStruct.h
    DESTINATION include
)

# 安装DLL文件
install(FILES
    thosttraderapi_se.dll
    thostmduserapi_se.dll
    DESTINATION bin
)

# 打印配置信息
message(STATUS "PYTHON_INCLUDE_DIRS: ${PYTHON_INCLUDE_DIRS}")
message(STATUS "PYTHON_LIBRARIES: ${PYTHON_LIBRARIES}")
message(STATUS "CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
message(STATUS "MSVC_VERSION: ${MSVC_VERSION}")